package com.foreknow.elm.dao.Impl;


import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con ;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Cart> listCart(Cart cart) throws SQLException {
        List<Cart> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append("select * FROM cart c,business b,food f WHERE c.businessId=b.businessId and c.foodId=f.foodId and userId=?");
        if (cart.getBusinessId() != null){
            sql.append(" and c.businessId="+cart.getBusinessId());
        }
        try {
            //Connection从ThreadLocal中获取
            Connection con = DBUtil.getConnection();
            pst = con.prepareStatement(String.valueOf(sql));
            pst.setString(1,cart.getUserId());

            //执行查询，并将查询到的结果放到结果集
            //遍历结果集并将每行数据保存到List集合中
            rs = pst.executeQuery();
            while (rs.next()){
                Cart cart1 = new Cart();
                Food food = new Food();
                Business business = new Business();
                cart1.setCartId(rs.getInt("cartId"));
                cart1.setQuantity(rs.getInt("quantity"));
                cart1.setFoodId(rs.getInt("foodId"));
                cart1.setBusinessId(rs.getInt("businessId"));
                cart1.setUserId(cart.getUserId());

                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));

                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                cart1.setBusiness(business);
                cart1.setFood(food);
                list.add(cart1);
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return list;
    }

    @Override
    public Integer saveCart(String userId, Integer businessId, Integer foodId) throws SQLException {
        int result = 0;
        String sql = "insert into cart(userId,businessId,foodId,quantity) values(?,?,?,1)";
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            pst.setInt(2, businessId);
            pst.setInt(3, foodId);
            result = pst.executeUpdate();
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return result;
    }

    @Override
    public Integer updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) throws SQLException {
        int result = 0;
        String sql = "update cart set quantity=? where userId=? and businessId=? and foodId=?";
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, quantity);
            pst.setString(2, userId);
            pst.setInt(3, businessId);
            pst.setInt(4, foodId);
            result = pst.executeUpdate();
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return result;
    }

    @Override
    public Integer removeCart(Cart cart) throws SQLException {
        int result = 0;
        StringBuffer sql = new StringBuffer();
        sql.append("delete from cart where userId=? and businessId=?");
        if (cart.getFoodId() !=  null) {
            sql.append(" and foodId="+cart.getFoodId());
        }
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(String.valueOf(sql));
            pst.setString(1, cart.getUserId());
            pst.setInt(2, cart.getBusinessId());
            result = pst.executeUpdate();
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return result;
    }
}